package com.ujiuye.dao;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class Homework01Dao {
    private QueryRunner qr = new QueryRunner(new ComboPooledDataSource());
    /**
     * 模糊搜索
     * @param search
     */
    public List<Object[]> dimQuery(String search) {
        System.out.println("search = " + search);
        String sql = "select uname from user where uname like '%" + search + "%'";
        /**
         * ArrayHandler     ArrayListHandler
         */
        try {
            List<Object[]> lists = qr.query(sql, new ArrayListHandler());
            return lists;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 验证用户名唯一
     * @param uname
     * @return
     */
    public int unameIsUnique(String uname) {
        String sql = "select count(*) from user where uname = ?";
        try {
            long l = (long)qr.query(sql, new ScalarHandler<>(),uname);
            return (int)l;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
